package com.higgins.server;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

import com.higgins.client.dataModel.RecipientDTO;
import com.higgins.client.dataModel.StrategyDTO;

@Entity
@Table(name = "recipients")
public class Recipient {

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
	private Long id;
     
    private String name;
    private String address;
    private String comment;
    
    @ManyToOne
    @JoinColumn(name="firmId")
    private LawFirm lawFirm;

    @ManyToOne
    @JoinColumn(name="sectorId")
    private Sector sector;

    @ManyToOne
    @JoinColumn(name="regionId")
    private Region region;
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="timestamp")
    private Date timestamp;

    public Recipient() {
        // This is used by JPA
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }


    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }
    
    public LawFirm getLawFirm()	{
    	return lawFirm;
    }
    
    public void setLawFirm(LawFirm lawFirm)	{
    	this.lawFirm = lawFirm;
    }

    public Sector getSector()	{
    	return sector;
    }
    
    public void setSector(Sector sector)	{
    	this.sector = sector;
    }
    public Region getRegion()	{
    	return region;
    }
    
    public void setRegion(Region region)	{
    	this.region = region;
    }

    
    public RecipientDTO toDTO()	{
    	RecipientDTO dto = new RecipientDTO();
    	dto.setId(id);
    	dto.setName(name);
    	dto.setAddress(address);
    	dto.setComment(comment);
    	dto.setTimestamp(timestamp);
    	if (lawFirm!=null)	{
    		dto.setLawFirmDTO(lawFirm.toDTO());
    	}
    	if (region!=null)	{
    		dto.setRegionDTO(region.toDTO());
    	}
    	if (sector!=null)	{
    		dto.setSectorDTO(sector.toDTO());
    	}
    	return dto;
    }
    public void update(RecipientDTO dto)	{
    	this.name = dto.getName();
    	this.address = dto.getAddress();
    	this.comment = dto.getComment();
    	this.timestamp = dto.getTimestamp();
    }

}